// lionfish_comshop/pages/user/Add shipping address.js
var util = require('../../utils/util.js');
var locat = require('../../utils/Location.js');
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    switch1Checked: false,
    is_default: '0',
    region: [],
    name: '',
    mobile: '',
    province_id: '',
    province_name:'',
    city_id: '',
    city_name:'',
    country_id: '',
    country_name:'',
    address: '',
    lon_lat:{},
  },
  switch1Change: function (e) {
    var show = e.detail.value
    var a
    if (show == true) {
      a = 1
    } else {
      a = 0
    }
    this.setData({
      switch1Checked: e.detail.value,
      is_default: a
    })
  },
  key_name(e) {
    this.setData({
      name: e.detail.value
    })
  },
  key_mobile(e) {
    this.setData({
      mobile: e.detail.value
    })
  },
  key_address(e) {
    this.setData({
      address: e.detail.value
    })
  },
  add_dz: function () {
    var token = wx.getStorageSync('token');
    var that = this
    if (that.data.name == '') {
      wx.showToast({
        title: '姓名不能为空，请先填写姓名',
        icon: 'none'
      })
      return false
    }
    if (that.data.mobile == '') {
      wx.showToast({
        title: '手机号不能为空，请先填写手机号',
        icon: 'none'
      })
      return false
    }
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (!myreg.test(that.data.mobile)) {
      wx.showToast({
        title: '手机号有误！',
      })
    }
    if (that.data.province_name == '') {
      wx.showToast({
        title: '所在地区不能为空，请先选择地区',
        icon: 'none'
      })
      return false
    }
    if (that.data.address == '') {
      wx.showToast({
        title: '详细地址不能为空，请先填写详细地址',
        icon: 'none'
      })
      return false
    }
    app.util.request({
      url: 'entry/wxapp/user',
      data: {
        controller: 'user.addaddress',
        token: token,
        name: that.data.name,
        telephone: that.data.mobile,
        province_id: that.data.province_id,
        city_id: that.data.city_id,
        country_id: that.data.country_id,
        address: that.data.address,
        is_default: that.data.is_default,
        lon_lat:that.data.lon_lat,
      },
      dataType: 'json',
      success: function (res) {
        console.log('res', res);
      }
    })
    wx.navigateBack()
  },
  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e)
    this.setData({
      province_id: e.detail.code[0],
      city_id: e.detail.code[1],
      country_id: e.detail.code[2],
      region: e.detail.value
    })
  },
  choseLocation: function () {
    let { tabAddress, tabIdx } = this.data;
    var that = this;
    wx.chooseLocation({
      success: function (e) {
        // console.log(e);
        var s_region = that.data.region;
        var filename = e.name;
        let addr = e.address || '';
        var reg = /.+?(省|市|自治区|自治州|县|区|特别行政区)/g;
        var result = addr.match(reg);
        var result = null;
        // console.log('patt', result);
        if (result == null || filename == "") {
          locat.getGpsLocation(e.latitude, e.longitude).then((res) => {
            console.log('反推了', res);
            if (res) {
              that.setData({
                province_id:res.ad_info.nation_code,
                city_id:res.ad_info.city_id,
                country_id:res.ad_info.adcode,
                province_name:res.ad_info.province,
                city_name:res.ad_info.city,
                country_name:res.ad_info.district,
                lon_lat:res.ad_info.location,
                address:res.address
              })
            }
          });
        } else {
          s_region[0] = result[0];
          s_region[1] = result[1];
          s_region[2] = result[2] || '';
          var dol_path = addr.replace(s_region.join(''), '');
          filename = dol_path + e.name;
          setRes();
        }
      },
      fail: function (error) {
        console.log(error)
        if (error.errMsg == 'chooseLocation:fail auth deny') {
          console.log('无权限')
          locat.checkGPS(app, locat.openSetting())
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})